﻿using Digitalmes.Domain.Entities.Mes.Masters;

namespace Digitalmes.Application.Mes.Masters.Materials.Commands;

/// <summary>
/// 更新物料命令。
/// </summary>
public record UpdateMaterialCommand(MdMaterialDto Material) : IRequest<IResult<bool>>;

internal sealed class UpdateMaterialCommandHandler(IRepository<MdMaterial> repo) : IRequestHandler<UpdateMaterialCommand, IResult<bool>>
{
    public async Task<IResult<bool>> Handle(UpdateMaterialCommand request, CancellationToken cancellationToken)
    {
        var ws = request.Material.Adapt<MdMaterial>();
        var ok = await repo.UpdateAsync(ws, m => new
        {
            m.Name,
            m.Type,
            m.Attr,
            m.Model,
            m.Spec,
            m.Unit,
            m.Rules,
            m.Supplier,
            m.IsCheckIQC,
            m.ManufacturingDate,
            m.WarehousingDate,
            m.Remark,
            m.Status,
            m.UpdatedBy,
            m.UpdatedTime,
        }, cancellationToken);
        return Result.From(ok);
    }
}
